package org.lanyu.springainovel.common.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lanyu.springainovel.common.dto.StreamAbortRequest;
import org.lanyu.springainovel.common.dto.StreamGenerationRequest;
import org.lanyu.springainovel.common.service.StreamGenerationService;
import org.lanyu.springainovel.common.vo.RestVO;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * 通用流式生成控制器
 */
@Slf4j
@RestController
@RequestMapping("/common/stream")
@RequiredArgsConstructor
@Tag(name = "通用流式生成接口", description = "提供通用的流式生成功能，可用于各种业务场景")
public class StreamGenerationController {

    private final StreamGenerationService streamGenerationService;

    /**
     * 通用流式生成接口
     * 
     * @param request 流式生成请求
     * @return 流式响应
     */
    @Operation(summary = "通用流式生成", description = "根据提供的参数生成流式响应，可用于生成灵感、正文、细纲等各种内容")
    @PostMapping(value = "/generate", produces = MediaType.TEXT_PLAIN_VALUE + ";charset=UTF-8")
    public Flux<String> generateStream(@RequestBody StreamGenerationRequest request) {
        log.info("收到通用流式生成请求，业务类型: {}, 模型ID: {}", request.getBusiness(), request.getModelId());
        return streamGenerationService.generateStreamResponse(request);
    }
    
    /**
     * 终止流式生成接口
     * 
     * @param request 终止请求
     * @return 终止结果
     */
    @Operation(summary = "终止流式生成", description = "根据终止信号终止对应的流式生成任务")
    @PostMapping("/abort")
    public RestVO<Boolean> abortStream(@RequestBody StreamAbortRequest request) {
        log.info("收到终止流式生成请求，终止信号: {}", request.getAbortSignal());
        boolean result = streamGenerationService.abortStreamGeneration(request.getAbortSignal());
        return RestVO.success(result);
    }
}